"""
法律文档下载脚本
从全国人大法律法规数据库(flk.npc.gov.cn)批量下载法律文档
支持多种法律文档类型的自动下载和格式转换
已废弃不能使用版本
getlaw->processlaws->selectlaws->getretrieverdataset->processretrieverdataset->sft or rlhf
"""

import requests
import os
from docx2pdf import convert
import subprocess

# ==================== 基础配置 ====================
# API基础URL
base_api_url = 'https://flk.npc.gov.cn/api/?'

current_script_path = os.path.abspath(__file__)
# 项目根目录（假设当前脚本位于 datasets/scripts 目录下）
project_root = os.path.dirname(os.path.dirname(os.path.dirname(current_script_path)))
# 构建目标路径
foldername = os.path.join(project_root, "data", "laws")

if not os.path.exists(foldername):
    os.makedirs(foldername)
    print(f"✓ 创建目录: {foldername}")

# ==================== 法律文档类型配置 ====================
# 法律文档类型映射：API代码 -> 中文名称
types = {
    # "flfg": "法律",           # 法律法规
    "jcfg": "监察法规"       # 监察法规
    # "sfjs": "司法解释",       # 司法解释
    # "xffl": "宪法",           # 宪法
    # "xzfg": "行政法规",       # 行政法规
    # "dfxfg": "地方性法规"     # 地方性法规
}

# 每种类型的页面数量（根据网站实际情况配置）
pages = {
    # "flfg": 70,     # 法律：70页
    "jcfg": 1      # 监察法规：1页
    # "sfjs": 86,     # 司法解释：86页
    # "xffl": 1,      # 宪法：1页
    # "xzfg": 80,     # 行政法规：80页
    # "dfxfg": 2492   # 地方性法规：2492页
}
# ==================== 主要下载逻辑 ====================
print("🚀 开始下载法律文档...")
for type in types:
    for page in range(1,pages[type]+1):
        headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        }
        mainurl=f"https://flk.npc.gov.cn/api/?page={page}&type={type}&searchType=title%3Baccurate&sortTr=f_bbrq_s%3Bdesc&gbrqStart=&gbrqEnd=&sxrqStart=&sxrqEnd=&sort=true&size=10&_=1752046246215"
        try:
            response = requests.get(url=mainurl)
            data_json = response.json()
        except Exception as e:
            print(e)
            print(response)
            print(page)
            continue
        try:
            for index in data_json['result']['data']:
                if index["type"]!=types[type] or index["status"]!="1":
                    continue
                id = index['id']
                title = index['title']
                url = 'https://flk.npc.gov.cn/api/detail'
                data = {
                    'id':id
                }
                new_data = requests.post(url=url,data=data,headers=headers).json()
                if new_data['result']['body'][0]['type']=='WORD':
                    ind = 0
                else:
                    ind = 1

                down_load = 'https://wb.flk.npc.gov.cn'+new_data['result']['body'][ind]['path']
                name = new_data['result']['body'][ind]['path'].split('.')[-1]
                content = requests.get(url=down_load,headers=headers).content
                docx_filename = foldername + '/' + title + '.docx'
                with open(docx_filename, mode='wb') as f:
                    f.write(content)
    
                pdf_filename = foldername + '/'  + title + '.pdf'
                convert(docx_filename, pdf_filename)
                subprocess.call("taskkill /im WINWORD.EXE /f", shell=True)   
        except Exception as e:
            print(e)
            try:
                print(title)
            except:
                print()    
        print(page)